// pages/index/scan.js
var app = getApp();
var __ctxPath = app.__ctxPath;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentYear: "",
    vo : {
      //主键、商品名、商品对应条形码、零售价、品牌、厂商、单位规格
      lid: '', 
      name: '',
      barcode: '',
      sellPrice: '',
      brand: '',
      supplier: '',
      standard: ''
    }
  },

  /**
   * 打开相机扫一扫，条形码
   */
  scan: function(){
    var that = this;
    wx.scanCode({
      onlyFromCamera: true, // 只允许从相机扫码
      success(res) {
        if ("scanCode:ok" != res.errMsg){
          alert("扫码错误");
          return;
        }

        wx.showLoading({ title: '数据加载中' });
        setTimeout(() => {
          //响应可能超时，为了提高用户体验，所以取消加载框
          wx.hideLoading();
        }, 5 * 1000);
        var barcode = res.result;
        try{
          wx.request({
            url: __ctxPath + '/outsideInterface/YxcOutsideInterface.do?method=getYxcGoods',
            data: { barcode: barcode },
            success: function (res) {
              //console.log(res.data);
              wx.hideLoading(); // 隐藏加载框
              if ("0" != res.data.code) {
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 2000//持续的时间
                });
                that.setData({
                  vo: { name: "暂未收录该商品" }
                })
              } else {
                that.setData({
                  vo: res.data.data ? res.data.data : { name: "暂未收录该商品" }
                })
              }
            }
          });
        }catch(err){
          //响应超时
          wx.hideLoading();
          that.setData({
            vo: { name: "暂未收录该商品" }
          })
        }
        
      }
    })
  },

 /**
  * 跳转到搜索商品页面
  */
  toGoodSearch: function(){
    wx.navigateTo({ url: '/pages/yxc/goodsSearchForScan/goodsSearchForScan'});
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      currentYear: new Date().getFullYear()
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  
  /**
   * title:分享标题； path：分型路径；desc：分享描述
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '裕兴昌扫码查价',
      path: 'pages/yxc/scan/scan',
      //desc: '来裕兴昌试试扫码查价吧~',
      success: function (res) {
        // 转发成功
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    }
  }

})